package yangsw87.swing;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.JTable;

import java.awt.Dimension;
import java.util.ArrayList;

import javax.swing.JScrollPane;

import yangsw87.dao.MemberDAO;
import yangsw87.model.MemberVO;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class ReadMember extends JDialog {

	private final JPanel contentPanel = new JPanel();
	private JTable table;
	private DefaultTableModel model = new DefaultTableModel();
	

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			ReadMember dialog = new ReadMember();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 */
	public ReadMember() {
		setBounds(100, 100, 640, 480);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setLayout(new FlowLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		{
			JScrollPane scrollPane = new JScrollPane();
			scrollPane.setPreferredSize(new Dimension(450, 400));
			contentPanel.add(scrollPane);
			{

				model = new DefaultTableModel() {
					
					boolean[] isEdit = new boolean[] {
							// 열의 속성을 (총 6개) false로 설정
							false, false, false, false, false, false };

					public boolean isCellEditable(int row, int col) {
						return isEdit[col]; // 모든 열 수정 불가
				}
				
				};
				
				model.setColumnIdentifiers(new String[] { "회원번호", "아이디",
						"비번", "이름", "성별", "가입일" });
							
				table = new JTable();
				table.setPreferredSize(new Dimension(0, 300));
				scrollPane.setViewportView(table);
				
				table.setModel(model);
			}
		}
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("조회");
				okButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						
						MemberDAO mdao = new MemberDAO();
						
						ArrayList<MemberVO> alist = mdao.selectMember();
						for (MemberVO mvo : alist) {
							model.addRow(new String[] { mvo.getMno() + " ",
									mvo.getUserid() + " ", mvo.getPwd() + " ",
									mvo.getName() + " ", mvo.getGender() + " ", mvo.getRegdate() + "" });
						}
									
					}
				});
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
		}
	}

}
